﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Continuum.Classes;

namespace Continuum
{
    public partial class GuidedSavings : Form
    {
        public GuidedSavings()
        {
            InitializeComponent();
        }

        private void GuidedSavings_Load(object sender, EventArgs e)
        {
            ddlSourceFund.DataSource = MainForm.projector.GetFundsOfClass(FundClassEnum.SpendingAccount);

            dtpInceptionDate.Value = DateTime.Now.Date.AddMonths(-6);
            dtpFirstSavings.Value = DateTime.Now.Date.AddMonths(-6);
        }

        private bool ValidateForm()
        {
            string ErrorList = "";

            txtSavingsName.Text = txtSavingsName.Text.Trim();

            // Validate Account Name
            if (txtSavingsName.Text == "")
                ErrorList += "- You need to provide a valid Savings Account Name\n";
            else
            {
                if (MainForm.projector.FindFund(txtSavingsName.Text) != null)
                    ErrorList += "- A Fund already exists with that Account Name!\n";
            }

            // Validate Inception Amount
            try { Decimal InceptionAmount = Decimal.Parse(txtInceptionAmount.Text); }
            catch { ErrorList += "- You need to provide a valid Inception Balance\n"; }

            // Validate Savings Plan items
            if (chkDoSavingsPlan.Checked)
            {
                // Validate Feeder Fund
                if (MainForm.projector.FindFund(ddlSourceFund.Text) == null)
                {
                    ErrorList += "- An invalid fund is selected for savings plan\n";
                }

                // Validate Savings plan Amount
                try { Decimal SavingsAmount = Decimal.Parse(txtSavingsAmount.Text); }
                catch { ErrorList += "- You need to provide a valid Savings Amount\n"; }

            }

            if (ErrorList == "") return true;

            MessageBox.Show("Please correct the following errors :\n" + ErrorList, "Unable to create Savings Account", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return false;
        }

        #region Button Handlers

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!ValidateForm()) return;

            Decimal InceptionAmount = Decimal.Parse(txtInceptionAmount.Text);

            Fund fund = new Fund(
                new Balance(InceptionAmount, dtpInceptionDate.Value.Date, "Savings Inception"),
                txtSavingsName.Text.Trim(), FundClassEnum.Savings, false);

            MainForm.projector.Funds.Add(fund);

            if (chkDoSavingsPlan.Checked)
            {
                Decimal SavingsAmount = Decimal.Parse(txtSavingsAmount.Text);

                Actor actor = new Actor("Savings Plan", txtSavingsName.Text.Trim(), ddlSourceFund.Text,
                    ActorPeriodicityEnum.Monthly, 1, dtpFirstSavings.Value.Date, SavingsAmount);

                MainForm.projector.Actors.Add(actor);
            }

            MainForm.InstanceRef.HasChanged = true;

            Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        #endregion

        #region Simple Numeric Textbox Filtering

        private void txtInceptionAmount_KeyDown(object sender, KeyEventArgs e)
        {
            if (!(e.KeyValue >= '0' && e.KeyValue <= '9') && !(e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9)
                && e.KeyCode != Keys.Decimal && e.KeyCode != Keys.Back && e.KeyCode != Keys.OemPeriod
                && e.KeyCode != Keys.Delete && e.KeyCode != Keys.Insert && e.KeyCode != Keys.OemMinus && e.KeyCode != Keys.Subtract
                && e.KeyCode != Keys.Left && e.KeyCode != Keys.Right
               ) e.SuppressKeyPress = true;
        }

        private void txtInterestAPR_KeyDown(object sender, KeyEventArgs e)
        {
            if (!(e.KeyValue >= '0' && e.KeyValue <= '9') && !(e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9)
                && e.KeyCode != Keys.Decimal && e.KeyCode != Keys.Back && e.KeyCode != Keys.OemPeriod
                && e.KeyCode != Keys.Delete && e.KeyCode != Keys.Insert && e.KeyCode != Keys.OemMinus && e.KeyCode != Keys.Subtract
                && e.KeyCode != Keys.Left && e.KeyCode != Keys.Right
               ) e.SuppressKeyPress = true;
        }

        private void txtSavingsAmount_KeyDown(object sender, KeyEventArgs e)
        {
            if (!(e.KeyValue >= '0' && e.KeyValue <= '9') && !(e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9)
                && e.KeyCode != Keys.Decimal && e.KeyCode != Keys.Back && e.KeyCode != Keys.OemPeriod
                && e.KeyCode != Keys.Delete && e.KeyCode != Keys.Insert && e.KeyCode != Keys.OemMinus && e.KeyCode != Keys.Subtract
                && e.KeyCode != Keys.Left && e.KeyCode != Keys.Right
               ) e.SuppressKeyPress = true;
        }

        #endregion

        private void lnkOnlineHelp_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo.UseShellExecute = true;
            proc.StartInfo.FileName = "http://project-continuum.com/OnlineHelp/GuidedSavings.aspx";
            proc.Start();
        }

    }
}
